package com.lyn.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;

/**
 * @author longyn 2023/10/10
 * @version 1.0.0
 */
public abstract class BaseActivity<V extends ViewBinding> extends AppCompatActivity {


    public Context context;

    public V binding;

    public ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.context = this;
        this.binding = getViewBinding();
        setContentView(this.binding.getRoot());
    }

    public Context getContext(){
        return context;
    }

    public abstract V getViewBinding();

    public void jump(Class<? extends Activity> activityClass, boolean finish) {
        Intent intent = new Intent(this.context, activityClass);
        startActivity(intent);
        if (finish) {
            finish();
        }
    }

    public void showProgressDialog(int style, String title, String message) {
        runOnUiThread(() -> {
            progressDialog = new ProgressDialog(context);
            progressDialog.setTitle(title);
            progressDialog.setMessage(message);
            progressDialog.setProgressStyle(style);
            progressDialog.setCanceledOnTouchOutside(false);
            progressDialog.show();
        });
    }

    public void setProgressDialogTitle(String title){
        runOnUiThread(() -> {
            if (progressDialog!=null){
                progressDialog.setTitle(title);
            }
        });
    }

    public void setProgressDialogMessage(String message){
        runOnUiThread(() -> {
            if (progressDialog!=null){
                progressDialog.setMessage(message);
            }
        });
    }

    public void setProgressDialogProgress(int progress){
        runOnUiThread(() -> {
            if (progressDialog!=null){
                progressDialog.setProgress(progress);
            }
        });
    }

    public void dismissProgressDialog(){
        runOnUiThread(() -> {
            if (progressDialog!=null){
                progressDialog.dismiss();
            }
        });
    }

}
